using System;
using System.IO;

namespace Engine.FileFormats3D.WR2TrafficFiles
{
    public struct Lane
    {
        #region Public Fields

        public short numberofLanes;
        public float Offset1;
        public float Offset2;

        #endregion Public Fields

        #region Private Fields

        private static int index = -1;
        private short flags;
        private int id;

        #endregion Private Fields

        #region Public Constructors

        public Lane(BinaryReader br, int NumberOfStreetpoint)
        {
            this.id = NumberOfStreetpoint + 1;
            this.Offset1 = br.ReadSingle();
            this.Offset2 = br.ReadSingle();
            this.flags = br.ReadInt16();
            this.numberofLanes = br.ReadInt16();

            if (this.numberofLanes != 1 && this.numberofLanes != 2)
                this.numberofLanes = 1;
        }

        public Lane(short numberOfLanes, float offsetStreet1, float offsetStreet2)
        {
            this.id = Lane.Index;
            this.Offset1 = offsetStreet1;
            this.Offset2 = offsetStreet2;
            this.numberofLanes = numberOfLanes;
            // flags: 4096 faster cars will overtake slower cars on 2 lane road.
            // flags: 12288 ?? is stable (no crashes of WR2)

            this.flags = 0;
            if (this.numberofLanes != 1 && this.numberofLanes != 2)
                throw new Exception("Number of Lanes must be 1 or 2");
        }

        #endregion Public Constructors

        #region Internal Constructors

        internal Lane(float offset1, float offset2, short flags, short numberOfLanes)
        {
            this.id = Lane.Index;
            this.Offset1 = offset1;
            this.Offset2 = offset2;
            this.flags = flags;
            this.numberofLanes = numberOfLanes;
            if (this.numberofLanes != 1 && this.numberofLanes != 2)
                this.numberofLanes = 1;
            //throw new Exception("Number of Lanes must be 1 or 2");
        }

        #endregion Internal Constructors

        #region Public Properties

        public static int Index
        {
            get
            {
                index++;
                return index;
            }
        }

        #endregion Public Properties

        #region Public Methods

        public static Lane FromStream(BinaryReader br)
        {
            return new Lane(br.ReadSingle(), br.ReadSingle(), br.ReadInt16(), br.ReadInt16());
        }

        public void ToStream(BinaryWriter bw)
        {
            if (this.numberofLanes != 1 && this.numberofLanes != 2)
                this.numberofLanes = 1;

            bw.Write(this.Offset1);
            bw.Write(this.Offset2);
            bw.Write(this.flags);
            bw.Write(this.numberofLanes);
        }

        public override string ToString()
        {
            return string.Format("Lanes: {0} O1: {1} O2: {2} Flag: {3}", this.numberofLanes, this.Offset1, this.Offset2, this.flags);
            //StringBuilder sb = new StringBuilder();
            //sb.AppendLine("-Lane------- " + index.ToString() + " ----------");
            //sb.AppendLine("Offest1:     " + Offset1.ToString());
            //sb.AppendLine("Offest2:     " + Offset2.ToString());
            //sb.AppendLine("flags:       " + flags.ToString());
            //sb.AppendLine("Lanes:       " + numberofLanes.ToString());
            //sb.AppendLine("-end-");
            //return sb.ToString();
        }

        #endregion Public Methods
    }
}